package com.easylib.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

import com.easylib.base.BaseConfig;


/**
 * 屏幕状态栏宽高，dp-px换算，屏幕截图
 */
public class ScreenUtils {

	/**
	 * 获得屏幕高度
	 * 返回像素
	 */
	public static int getScreenWidth() {
		WindowManager wm = (WindowManager) BaseConfig.application.getSystemService(
				Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics.widthPixels;
	}

	/**
	 * 获得屏幕宽度
	 * 返回像素
	 */
	public static int getScreenHeight() {
		WindowManager wm = (WindowManager) BaseConfig.application.getSystemService(
				Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics.heightPixels;
	}

	/**
	 * 获取状态栏高度 单位px 可置于setContentView()之前
	 */
	public static int getStatusBarHeight() {
//	    Resources resources = con.getResources();
		Resources resources =Resources.getSystem();
	    int resourceId = resources.getIdentifier("status_bar_height", "dimen","android");
	    int height = resources.getDimensionPixelSize(resourceId);
	    return height;
	}
	
	/**
	 * 获取虚拟键盘高度
	 */
	public static int getNavigationBarHeight() {
//	    Resources resources = con.getResources();
	    Resources resources =Resources.getSystem();
	    int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android");
	    int height = resources.getDimensionPixelSize(resourceId);
	    return height;
	}

	/**
	 * 获取当前屏幕截图，包含状态栏
	 */
	public static Bitmap snapShotWithStatusBar(Activity activity) {
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bmp = view.getDrawingCache();
		int width = getScreenWidth();
		int height = getScreenHeight();
		Bitmap bp = null;
		bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
		view.destroyDrawingCache();
		return bp;

	}

	/**
	 * 获取当前屏幕截图，不包含状态栏
	 */
	public static Bitmap snapShotWithoutStatusBar(Activity activity) {
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bmp = view.getDrawingCache();
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;

		int width = getScreenWidth();
		int height = getScreenHeight();
		Bitmap bp = null;
		bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
				- statusBarHeight);
		view.destroyDrawingCache();
		return bp;

	}

	/**
	 * DP转换成像素
	 */
	public static int dpToPx(int dp)
	{
	    return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
	}

	/**
	 * 像素转换成DP
     */
	public static int pxToDp(int px)
	{
	    return (int) (px / Resources.getSystem().getDisplayMetrics().density);
	}
	/**
     * 将px值转换为sp值，保证文字大小不变
     *            （DisplayMetrics类中属性scaledDensity）
     */
    public static int px2sp(float pxValue) { 
        final float fontScale = Resources.getSystem().getDisplayMetrics().scaledDensity; 
        return (int) (pxValue / fontScale + 0.5f); 
    } 
   
    /**
     * 将sp值转换为px值，保证文字大小不变
     *            （DisplayMetrics类中属性scaledDensity）
     */
    public static int sp2px(float spValue) { 
        final float fontScale = Resources.getSystem().getDisplayMetrics().scaledDensity; 
        return (int) (spValue * fontScale + 0.5f); 
    }  
}
